package com.zry.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zry.config.userlocal.UserThreadLocal;
import com.zry.constant.KConstant;
import com.zry.entity.Blog;
import com.zry.entity.User;
import com.zry.enums.StatusEnum;
import com.zry.service.blog.IBlogService;
import com.zry.service.blogCategoty.IBlogCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.swing.text.html.Option;
import java.util.Optional;

/**
 * @author Zry
 */

@Controller
@Slf4j
public class BlogController {

    @Autowired
    private IBlogService blogService;
    /* ioc注入静态成员变量
    private static IBlogService blogService;

    @Autowired
    public void setBlogService(IBlogService blogService) {
        BlogController.blogService = blogService;
    }*/


    @Autowired
    private IBlogCategoryService blogCategoryService;

    @GetMapping("/blog")
    public ModelAndView index(String cid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cid", Optional.ofNullable(cid).orElse("0"));
        modelAndView.setViewName("blog/index");
        return modelAndView;
    }

    @GetMapping("/blog/add")
    public ModelAndView toAdd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/blog/add");
        return modelAndView;
    }

    @GetMapping("/blog/edit/{blogId}")
    public ModelAndView toEdit(@PathVariable("blogId") Integer blogId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/blog/add");
        modelAndView.addObject("blogId", blogId);
        return modelAndView;
    }

    @GetMapping("/api/blog/get/{blogId}")
    @ResponseBody
    public Blog getBlogById(@PathVariable("blogId") Integer blogId){
        Optional.ofNullable(blogId).orElseThrow(()->new RuntimeException("文章不存在"));
        Blog blog = blogService.getById(blogId);
        Optional.ofNullable(blog).orElseThrow(()->new RuntimeException("文章不存在"));
        return blog;
    }


    @PostMapping("/api/blog/saveOrUpdate")
    @ResponseBody
    public Blog saveOrUpdateBlog(@RequestBody Blog blog){

        if(blog.getId()==null) {
            blog.setUserId(UserThreadLocal.get().getId());
            blog.setStatus(StatusEnum.PUBLISH);
            blog.setIsDelete(0);
        }

       return blogService.saveOrUpdate(blog)?blog:null;
    }

    @GetMapping("/api/blog/load")
    @ResponseBody
    public Page<Blog> getBlogList(@RequestParam(required = false, defaultValue = "1") Integer pageNo
            , @RequestParam(required = false, defaultValue = "10") Integer pageSize
            , @RequestParam(required = false, defaultValue = "0") Integer cid
            , @RequestParam(required = false, defaultValue = "0") Integer sortNum) {
        Page<Blog> page = new Page<>(pageNo, pageSize);

        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                // cid为空则不进行过滤分类
                .eq(cid != null && !cid.equals(0), Blog::getCategoryId, cid)
                .eq(Blog::getStatus, 1)
                .eq(Blog::getIsDelete, 0);
        if(sortNum.equals(0) || sortNum.equals(2)) {
            queryWrapper.orderByDesc(Blog::getCreateTime);
        }else {
            queryWrapper.orderByAsc(Blog::getCreateTime);
        }
        return blogService.page(page, queryWrapper);
    }

    @GetMapping("/detail/{blogId}")
    public ModelAndView detail(@PathVariable("blogId") Integer blogId){

        ModelAndView modelAndView = new ModelAndView();

        Blog blog = blogService.getById(blogId);

        modelAndView.setViewName("blog/detail");
        modelAndView.addObject("blog", blog);

        return modelAndView;
    }

}
